<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\Major;
use App\Models\Teacher;
use App\Models\Student;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;


class TeacherGradeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('我的班级')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('班级学生')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Grade);

        $user_id = Admin::user()->id;
        $teacher = Teacher::where('user_id',$user_id)->first();
        $teacher_id = $teacher->id;
        $grid->model()->where('class_teacher', $teacher_id);

        $grid->model()->orderBy('id','desc');
        $grid->id('Id')->sortable();
        $grid->major_id('所属专业')->display(function ($major_id) {
            $major = Major::where('id', $major_id)->first();
            return $major->title;
        });
        $grid->class_code('班级编码');
        $grid->class_name('班级名称');

        $grid->column('班级人数')->display(function () {
            $counts = Student::where('class_id', $this->id)->where('id','>','0')->count();
            return $counts;
        });
        $grid->created_at('创建日期');
        $grid->updated_at('修改日期');
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
//            $actions->disableView();
        });
        $grid->paginate(10);
        $grid->disableCreateButton();
        $grid->disableFilter();
//        $grid->disableActions();
        $grid->disableRowSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
//        $show = new Show(Student::findOrFail($id));
        $grid = new Grid(new Student);
        $grid->model()->where('class_id', $id)->where('id','>',0);
//        $grid->id('Id')->sortable();
        $grid->seat('座位号')->sortable();
        $grid->student_id('学号');
        $grid->name('姓名');
        $grid->sex('性别')->display(function ($sex) {
            $sexStr = '';
            $color = '#D4D8DB';
            switch ($sex) {
                case '0':
                    $color = '#587EB7';
                    $sexStr = '男';
                    break;
                case '1':
                    $color = '#CE4C92';
                    $sexStr = '女';
                    break;
                case '2':
                    $sexStr = '其他';
                    break;
            }
            return "<span style='color: $color'>$sexStr</span>";
        });
        $grid->birthday('生日');
        $grid->nation('民族');
        $grid->id_card('身份证');
        $grid->mobile('手机');
        $grid->qq('QQ');
        $grid->boarder('是否住宿')->display(function ($boarder) {
            $boarder ? $boarderStr = '住宿生': $boarderStr = '走读生';
            return $boarderStr;
        });
        $grid->father_name('父亲');
        $grid->father_phone('联系方式');
        $grid->mother_name('母亲');
        $grid->mother_phone('联系方式');


        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

        $grid->tools(function ($tools) {
            $tools->prepend('<a class="btn btn-sm btn-primary" href="javascript:history.go(-1)" onclick=""><i class="fa fa-reply"></i>&nbsp;&nbsp;返回</a>');
        });


        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Grade);

        return $form;
    }
}
